package com.hollysys.usersync.configure;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import com.hollysys.usersync.feign.AuthServiceInvoker;
import com.hollysys.usersync.feign.OrgServiceInvoker;
import com.hollysys.usersync.feign.LoginServiceInvoker;
import feign.Feign;
import feign.codec.ErrorDecoder;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import feign.okhttp.OkHttpClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 平台服务feign自动配置
 *
 * @author Jeremy Yim
 */
@Configuration
@ConditionalOnClass({AuthServiceInvoker.class, OrgServiceInvoker.class, LoginServiceInvoker.class})
@EnableConfigurationProperties(PlatformProperties.class)
public class FeignConfiguration {

    @Resource
    private PlatformProperties properties;

    @Bean
    public AuthServiceInvoker authServiceInvoker() {
        ObjectMapper mapper = jsonMapper();
        Feign.Builder builder = Feign.builder()
                .client(new OkHttpClient())
                .encoder(new JacksonEncoder(mapper))
                .decoder(new JacksonDecoder(mapper))
                .options(new feign.Request.Options(properties.getConnectTimeoutMillis(), properties.getReadTimeoutMillis()))
                .errorDecoder(new ErrorDecoder.Default());
        return builder.target(AuthServiceInvoker.class, properties.getAuthServiceUrl());
    }

    @Bean
    public OrgServiceInvoker orgServiceInvoker() {
        ObjectMapper mapper = jsonMapper();
        Feign.Builder builder = Feign.builder()
                .client(new OkHttpClient())
                .encoder(new JacksonEncoder(mapper))
                .decoder(new JacksonDecoder(mapper))
                .options(new feign.Request.Options(properties.getConnectTimeoutMillis(), properties.getReadTimeoutMillis()))
                .errorDecoder(new ErrorDecoder.Default());
        return builder.target(OrgServiceInvoker.class, properties.getSystemServiceUrl());
    }

    @Bean
    public LoginServiceInvoker loginServiceInvoker() {
        ObjectMapper mapper = jsonMapper();
        Feign.Builder builder = Feign.builder()
                .client(new OkHttpClient())
                .encoder(new JacksonEncoder(mapper))
                .decoder(new JacksonDecoder(mapper))
                .options(new feign.Request.Options(properties.getConnectTimeoutMillis(), properties.getReadTimeoutMillis()))
                .errorDecoder(new ErrorDecoder.Default());
        return builder.target(LoginServiceInvoker.class, properties.getSsoServiceUrl());
    }

    private ObjectMapper jsonMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        mapper.registerModule(new JavaTimeModule());
        mapper.registerModule(new ParameterNamesModule());
        mapper.registerModule(new Jdk8Module());
        return mapper;
    }

}

